import { CatConfig } from "../configs/CatConfig";
import { GameCat } from "../GameCat";
import { CatDir, GameCatAnimData } from "../GameDefine";

/**
 * 动画管理
 * @Author: lzmxqh  
 * @Date: 2022-10-23 18:06:05  
 * @Last Modified by: lzmxqh
 * @Last Modified time: 2022-10-23 22:12:14
 */
export class AnimMgr {
    private static inst: AnimMgr;

    public static getInst(): AnimMgr {
        AnimMgr.inst = AnimMgr.inst || new AnimMgr();
        return AnimMgr.inst;
    }

    /**获取动画键值 */
    public static getAnimKey(animName: string, isStep: boolean): string {
        return animName + "_" + (isStep ? "step" : "run");
    }

    /**加载动画 */
    public loadAnims(scene: Phaser.Scene): void {
        const anims: Phaser.Animations.AnimationManager = scene.anims;

        // 加载猫帧动画
        const catConfig: { [dir: number]: GameCatAnimData } = CatConfig.CAT_CONFIG;
        for (let dir in catConfig) {
            this.addCatAnim(anims, catConfig[dir].animName);
        }
    }

    /**添加猫-帧动画 */
    private addCatAnim(anims: Phaser.Animations.AnimationManager, animName: string): void {
        anims.create(CatConfig.CAT_ANIM_CONFIG[AnimMgr.getAnimKey(animName, true)]);
        anims.create(CatConfig.CAT_ANIM_CONFIG[AnimMgr.getAnimKey(animName, false)]);
    }

    /**播放猫-帧动画 */
    public playCatAnim(cat: GameCat, dir: CatDir, isStep: boolean, ignoreIfPlaying?: boolean): void {
        const key: string = AnimMgr.getAnimKey(CatConfig.CAT_CONFIG[dir].animName, isStep);
        cat.model.play(key, ignoreIfPlaying);
    }
}
